package com.woniuxy.controller;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Role;
import com.woniuxy.entity.RoleMenuDTO;
import com.woniuxy.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色接口")
@RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
public class RoleController {
    @Autowired(required = false)
    RoleService roleService;

    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取分页角色数据")
    public ResponseEntity findAll(@ApiParam(name = "currentPage", value="当前页码",required=true)@PathVariable int currentPage,
                                  @ApiParam(name = "pageSize", value="页面大小",required=true)@PathVariable int pageSize){
        return roleService.findAll(currentPage, pageSize);
    }

    @RequiresPermissions(value = "role:add")
    @RequiresRoles(value = "superAdmin")
    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public ResponseEntity addOne(@ApiParam(name = "role", value="角色实体",required=true) @RequestBody Role role){
        return roleService.addOne(role);
    }

    @RequiresPermissions(value = "role:update")
    @RequiresRoles(value = "superAdmin")
    @PutMapping("/update")
    @ApiOperation(value = "修改角色")
    public ResponseEntity updateOne(@ApiParam(name = "role", value="角色实体",required=true) @RequestBody Role role){
        return roleService.updateOne(role);
    }

    @RequiresPermissions(value = "role:delete")
    @RequiresRoles(value = "superAdmin")
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除角色")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="角色id",required=true) @PathVariable int id){
        return roleService.deleteOne(id);
    }

    @PostMapping("/search")
    @ApiOperation(value = "查询角色")
    public ResponseEntity findByContext(@ApiParam(name = "role", value="角色实体",required=true) @RequestBody Role role){
        return roleService.findByContext(role);
    }

    @GetMapping("/checked/{roleId}")
    @ApiOperation(value = "获取用户已勾选角色")
    public ResponseEntity checkedRole(@PathVariable int roleId){
        return roleService.checkedRole(roleId);
    }

    @PostMapping("/schangeMenus")
    @ApiOperation(value = "更新角色")
    public ResponseEntity changeMenus(@RequestBody RoleMenuDTO roleMenuDTO){
        return roleService.changeMenus(roleMenuDTO);
    }

    @RequiresPermissions(value = "role:batchDelete")
    @RequiresRoles(value = "superAdmin")
    @PutMapping("/batchDelete")
    @ApiOperation(value = "批量删除角色")
    public ResponseEntity batchDelete(@ApiParam(name = "ids", value="角色ids",required=true) @RequestBody int[] ids){
        return roleService.batchDelete(ids);
    }

    @RequiresRoles(value = "superAdmin")
    @GetMapping("/all")
    @ApiOperation(value = "获取所有角色")
    public ResponseEntity findAll(){
        return roleService.findAll();
    }

    @RequiresRoles(value = "superAdmin")
    @GetMapping("/roles/{id}")
    @ApiOperation(value = "获取当前用户角色")
    public ResponseEntity getRoles(@ApiParam(name = "id", value="角色id",required=true) @PathVariable int id){
        return roleService.getRoles(id);
    }

    @RequiresRoles(value = "superAdmin")
    @PutMapping("/roles/{id}/{roles}")
    @ApiOperation(value = "更改用户角色")
    public ResponseEntity updateRoles(@ApiParam(name = "id", value="用户id",required=true) @PathVariable int id,
                                      @ApiParam(name = "roles", value="角色ids",required=true) @PathVariable String roles){
        return roleService.updateRoles(id,roles);
    }
}
